#include "StdAfx.h"
#include "Network_Handler.h"

Network_Handler::Network_Handler(void)
{
}

String^ Network_Handler::Send_XML()
{
// 	String^ server = "143.160.141.165";
// 	Int32 port = 4016;
// 	TcpClient^ client = gcnew TcpClient(server,port);
// 	NetworkStream^ stream = client->GetStream();
// 	String^ user = "216",^ pass = "pass";
// 	array<Byte>^ data = Text::Encoding::ASCII->GetBytes("<Artemis><query><type>DIRECT</type><data>SELECT * FROM `User` LIMIT 1</data></query></Artemis>");
// 	stream->Write(data, 0, data->Length);
//  	data = gcnew array<Byte>(8192);
// 	System::Int32 bytes = stream->Read(data, 0, data->Length);
//     s = Text::Encoding::ASCII->GetString(data, 0, bytes);
	Socket^ soc = Network_Handler::Connect_Server();
	if (soc != nullptr)
	{
		array<Byte>^ data = Encoding::UTF8->GetBytes("<Artemis><query><type>DIRECT</type><data>SELECT * FROM `User` LIMIT 1</data></query></Artemis>");
		soc->Send(data);
		data = gcnew array<Byte>(8192);
		int count = soc->Receive(data);
		return Encoding::UTF8->GetString(data);
	}
	else
	{

	}
}

Socket^ Network_Handler::Connect_Server()
{
#ifdef _DEBUG
	String^ output = "";
	output += "== Network_Handler::Connect_Server()\n";
	output += "====================================\n";
	for each(System::String ^aValue in System::Configuration::ConfigurationManager::AppSettings)
	{
		output += " -- Setting: " + aValue + " -> " 
			+  System::Configuration::ConfigurationManager::AppSettings[aValue] + "\n";
	}
	System::Windows::Forms::MessageBox::Show(output);
#endif // _DEBUG
 	Socket^ soc = gcnew Socket (AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp);
	IPAddress^ ip = IPAddress::Parse (System::Configuration::ConfigurationManager::AppSettings["server_ip"]);
	int port = int::Parse (System::Configuration::ConfigurationManager::AppSettings["server_port"]);
	soc->Connect(ip, port);
 	return soc;
}

void Network_Handler::Send_Server()
{

}

void Network_Handler::Recieve_Server()
{

}
